package logic

import (
	"context"
	"time"

	"rpcjwt/api/internal/svc"
	"rpcjwt/api/internal/types"

	"github.com/golang-jwt/jwt/v4"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 登录获取token
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
	// todo: add your logic here and delete this line
	mySigningKey := []byte("XyesjsjjsksuM")
	type MyCustomClaims struct {
		Userid string `json:"userid"`
		jwt.RegisteredClaims
	}
	userid := "144551222554"
	claims := MyCustomClaims{
		userid,
		jwt.RegisteredClaims{
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
			Issuer:    "alsark",
		},
	}

	tokeny := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	token, _ := tokeny.SignedString(mySigningKey)

	return &types.LoginResp{
		Token: token,
	}, nil
}
